package com.sziit.se.highconcurrency.tool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisUtils {
    private StringRedisTemplate stringRedisTemplate;

    public RedisUtils() {}

    @Autowired
    public RedisUtils(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 指定缓存失效时间
     *
     * @param key      键
     * @param time     时间
     * @param timeUnit 时间类型 秒TimeUnit.MINUTES、分钟TimeUnit.SECONDS、天TimeUnit.DAYS等
     * @return 是否成功
     */
    public boolean expire(String key, long time, TimeUnit timeUnit) {
        try {
            if (time > 0) {
                stringRedisTemplate.expire(key, time, timeUnit);
            }
            return true;
        } catch (Exception e) {
            log.error("设置缓存失效时间失败:{}", e.getMessage());
            return false;
        }
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return 判断结果
     */
    public boolean hasKey(String key) {
        try {
            Boolean result = stringRedisTemplate.hasKey(key);
            return result != null && result;
        } catch (Exception e) {
            log.warn("判断key是否存在出现异常:{}", e.getMessage());
            return false;
        }
    }

    /**
     * 普通缓存放入并设置时间
     *
     * @param key      键
     * @param value    值
     * @param time     失效时间 time小于等于0将设置无限期
     * @param timeUnit 时间类型 秒TimeUnit.MINUTES、分钟TimeUnit.SECONDS、天TimeUnit.DAYS等
     * @return 是否成功
     */
    public boolean set(String key, String value, long time, TimeUnit timeUnit) {
        try {
            if (time > 0) {
                stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error("设置指定失效时间缓存失败：{}", e.getMessage());
            return false;
        }
    }


    /**
     * 普通缓存放入
     *
     * @param key   键
     * @param value 值
     * @return 是否成功
     */
    public boolean set(String key, String value) {
        try {
            stringRedisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("设置缓存失败:{}", e.getMessage());
            return false;
        }
    }

    /**
     * 普通缓存获取
     *
     * @param key 键
     * @return 值
     */
    public String get(String key) {
        return key == null ? null : stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 删除缓存
     *
     * @param keys 键
     */
    public boolean del(String... keys) {
        if (keys != null && keys.length > 0) {
            if (keys.length == 1) {
                Boolean result = stringRedisTemplate.delete(keys[0]);
                return result != null && result;
            } else {
                Long aLong = stringRedisTemplate.delete(Arrays.asList(keys));
                return aLong != null && aLong == keys.length;
            }
        }
        return false;
    }

    /**
     * 根据用户id和用户类型生成key
     *
     * @param userId     用户id
     * @param clientType 用户类型
     * @return key
     */
    public String generatorKey(Long userId, int clientType) {
        return userId + "_" + clientType;
    }
}
